<template>
  <div class="ele-body" :style="id==undefined?'':'padding:0;margin-left:-15px'">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" ref="formRef" class="ele-form-search" label-width="auto" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="问题搜索：" prop="keyword">
              <el-input v-model="where.keyword" clearable placeholder="请输入关键词"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12" v-if="id==undefined">
            <el-form-item label="项目名称：" label-width="100px" prop="project_id">
              <ProjectSelect v-model="where.project_id" :unStart="true"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:projectList:index')" class="ele-btn-icon" icon="el-icon-search"
                         type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :where="where"
                     :height="`calc(100vh - ${id != undefined?375:315}px)`">
        <template slot="resolve" slot-scope="{row}">
          {{ row ? row.resolve_user_names.join(',') : '' }}
        </template>

        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="$refs.addFeedbackRef.visible=true">
            添加问题
          </el-button>
<!--          <el-dropdown @command="handleCommand">-->
<!--            <el-button icon="el-icon-plus" size="small" type="primary"-->
<!--                       >-->
<!--              添加问题-->
<!--            </el-button>-->
<!--            <el-dropdown-menu slot="dropdown">-->
<!--              <el-dropdown-item v-for="item in typeList" :key="item" :command="item">{{ item }}</el-dropdown-item>-->
<!--            </el-dropdown-menu>-->
<!--          </el-dropdown>-->

          <el-button size="small" type="primary" :loading="exportLoading" @click="exportData" v-if="id==undefined">
            数据导出
          </el-button>
        </template>
        <template #address="{row}">
          <span>{{ row.province_name }} {{ row.city_name }} {{ row.county_name }} {{ row.address }}</span>
        </template>

        <template slot="level" slot-scope="{row}">
          <el-tag
            :type="row.level==='一般'?'success':row.level==='严重'?'':row.level==='非常严重'?'warning':'danger'"
            effect="dark">
            {{ row.level }}
          </el-tag>
        </template>

        <template #action="{row}" :destroy-on-close="true">
          <div style="display: flex;justify-content: space-around">
            <el-button icon="el-icon-search" type="text" @click="onCheck(row)" :disabled="!row.button_show">查看
            </el-button>
            <el-button icon="el-icon-edit" type="text" @click="onEdit(row)" :disabled="!row.button_modify">编辑
            </el-button>
            <el-button icon="el-icon-finished" type="text" :disabled="!row.button_done"
                       @click="completeFeedback(row.id)">完成
            </el-button>
            <el-popconfirm
              title="确定关闭？"
              @confirm="closeFeedback(row.id)"
              placement="top"
            >
              <el-button icon="el-icon-switch-button" slot="reference" type="text" :disabled="!row.button_close">关闭
              </el-button>
            </el-popconfirm>
            <el-popconfirm
              title="确定删除？"
              @confirm="delFeedback(row.id)"
              placement="top"
            >
              <el-button icon="el-icon-delete" slot="reference" type="text" style="color: #f56c6c"
                         :disabled="!row.button_delete">删除
              </el-button>
            </el-popconfirm>
          </div>
        </template>
      </ele-pro-table>
    </el-card>
    <CompleteFeedback ref="completeFeedbackRef" :category="category" @reload="reload"></CompleteFeedback>
    <AddFeedback ref="addFeedbackRef" :level="level" :category="category" :source="source" :id="id" @reload="reload"/>
    <EditFeedback ref="editFeedbackRef" :level="level" :category="category" :source="source" :id="id" :row="row"
                  @reload="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import AddFeedback from "@/views/project/feedback/components/add-feedback.vue";
import EditFeedback from "@/views/project/feedback/components/edit-feedback.vue";
import CompleteFeedback from "@/views/project/feedback/components/complete-feedback.vue";
import ProjectSelect from "@/views/project/task/components/ProjectSelect.vue";


export default {
  props: ['id'],
  components: {
    AddFeedback,
    CompleteFeedback,
    EditFeedback,
    ProjectSelect
  },
  computed: {
    ...mapGetters(["permission"]),
  },

  data() {
    return {
      // 表格数据接口
      url: '/suggests',
      where: {
        keyword: null,
        project_id: this.id
      },
      loading: false,
      // 表格列配置
      columns: [
        {
          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'title',
          label: '问题标题',
          minWidth: 250,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          slot: 'level',
          label: '问题级别',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'project_name',
          label: '绑定项目',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'center',
        },
        {
          prop: 'task_name',
          label: '所属任务',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'center',
        },
        {
          prop: 'create_user_name',
          label: '问题提出人',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'charge_user_name',
          label: '问题负责人',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'create_time',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center'
        },
        {
          slot: 'resolve',
          label: '解决人',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'center',
        },
        {
          prop: 'status_text',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'resolve_time',
          label: '处理时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 320,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      level: [],
      category: [],
      source: [],
      row: {}, //单行数据
      exportLoading:false,
      typeList:['质量问题','非质量问题']
    };
  },
  mounted() {
    this.getParams()
  },
  methods: {
    /*导出数据*/
    exportData() {
      this.exportLoading = true
      this.$http.get('/suggests/export?limit=9999').then((res) => {
        this.exportLoading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          const link = document.createElement('a')
          //_blank表示在新窗口打开链接
          // link.target = '_blank'
          link.href = res.data.data.url
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    getParams() {
      this.loading = true
      this.$http.get('/suggests/params').then((res) => {
        this.loading = false;
        if (res.data.code === 0) {
          this.level = res.data.data.level
          this.category = res.data.data.category
          this.source = res.data.data.source
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    reset() {
      this.$refs.formRef.resetFields();
      this.reload();
    },
    onCheck(row) {
      this.$router.push({path: '/project/feedback/info', query: {id: row.id}})
    },
    onEdit(row) {
      this.row = row
      this.$refs.editFeedbackRef.visible = true
    },
    completeFeedback(id) {
      this.$refs.completeFeedbackRef.visible = true
      this.$refs.completeFeedbackRef.id = id
    },
    delFeedback(id) {
      this.loading = true
      this.$http.delete(`/suggests/${id}`).then((res) => {
        this.loading = false;
        if (res.data.code === 0) {
          this.reload()
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    handleCommand(val){
      console.log(val)
    },
    closeFeedback(id) {
      this.loading = true
      this.$http.post(`/suggests/${id}/close`).then((res) => {
        this.loading = false;
        if (res.data.code === 0) {
          this.reload()
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-table__body {
  width: 100%;
  // 使表格兼容safari，不错位
  table-layout: fixed !important;
}
</style>

